<?php
/* * *******************************************************************************
 * Copyright (C) 2011-2013 X2Engine Inc. All Rights Reserved.
 * 
 * X2Engine Inc.
 * P.O. Box 66752
 * Scotts Valley, California 95067 USA
 * 
 * Company website: http://www.x2engine.com 
 * Community and support website: http://www.x2community.com 
 * 
 * X2Engine Inc. grants you a perpetual, non-exclusive, non-transferable license 
 * to install and use this Software for your internal business purposes.  
 * You shall not modify, distribute, license or sublicense the Software.
 * Title, ownership, and all intellectual property rights in the Software belong 
 * exclusively to X2Engine.
 * 
 * THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTIES OF ANY KIND, EITHER 
 * EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED WARRANTIES OF 
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE, AND NON-INFRINGEMENT.
 * ****************************************************************************** */

/* @edition:pro */
?>
<div class="span-16">
	<div class="page-title"><h2><?php echo Yii::t('admin', 'Email Capture'); ?></h2></div>
	<div class="form">
		<div class="row">
			<h3><?php echo Yii::t('admin', 'Introduction'); ?></h3>
			<?php echo Yii::t('admin', 'This feature allows X2CRM to receive and record emails at a special address.'); ?><br /><br />
			<?php echo Yii::t('admin', 'Enabling this feature requires that this web server have a mail transfer agent such as Postfix or Exim4 installed on it. It also requires some extra server-end configuration.'); ?>
			<?php echo Yii::t('admin', 'Before beginning, please see the {emailConfig} page on the X2CRM Wiki.', array('{emailConfig}' => CHtml::link(Yii::t('admin', 'Email Configuration'), 'http://wiki.x2engine.com/wiki/E-Mail_Configuration'))); ?>
		</div>
		<br /><hr />
		<div class="row">
			<h3><?php echo Yii::t('admin', 'Filesystem ownership and permissions'); ?></h3>
			<?php echo Yii::t('admin', 'The mail transfer agent must have read and write permissions to the directory "{path}". The ownership and permissions of {path} are as follows:', array('{path}' => 'protected/runtime')); ?><br /><br />
			<div class="cell span-3">
				<h4><?php echo Yii::t('admin', 'Ownership'); ?></h4>
				<?php
				$runtime = realpath(Yii::app()->basePath . '/runtime');
				echo "<strong>" . Yii::t('admin', 'User') . "</strong>:" . fileowner($runtime) . '<br />';
				echo "<strong>" . Yii::t('admin', 'Group') . "</strong>:" . filegroup($runtime) . '<br />';
				?>
			</div>
			<div class="cell span-6">
				<h4><?php echo Yii::t('admin', 'Permissions'); ?></h4>
				<?php
				// Perform a preliminary check of the runtime folder
				$mode = fileperms($runtime);
				$perms = array_fill_keys(array('Owner', 'Group', 'Any'), array());

				$info = '';
				$permTypes = array_fill_keys(array('Owner', 'Group', 'Any'), null);
				$permLabels = array_fill_keys(array('read', 'write', 'execute'), false);
				$permBits = array(
					'Owner' => array(
						'read' => 0x0100,
						'write' => 0x0080,
						'execute' => array(0x0040, 0x0800),
					),
					'Group' => array(
						'read' => 0x0020,
						'write' => 0x0010,
						'execute' => array(0x0008, 0x0400),
					),
					'Any' => array(
						'read' => 0x0004,
						'write' => 0x0002,
						'execute' => array(0x0001, 0x0200),
					),
				);

				foreach (array_keys($permTypes) as $type) {
					$permTypes[$type] = Yii::t('admin', $type);
					foreach (array_keys($permLabels) as $label) {
						if (!$permLabels[$label])
							$permLabels[$label] = Yii::t('admin', $label);
						if ($label == 'execute') {
							if (($mode & $permBits[$type][$label][0]) && !($mode & $permBits[$type][$label][1])) {
								$perms[$type][] = $label;
							}
						} else {
							if ($mode & $permBits[$type][$label]) {
								$perms[$type][] = $label;
							}
						}
					}
					if (empty($perms[$type]))
						$perms[$type][] = '(' . Yii::t('admin', 'no permission') . ')';
				}
				$permInfo = '';
				foreach ($perms as $type => $rwx) {
					$permInfo .= "<strong>{$permTypes[$type]}:</strong> " . implode(',', $rwx) . '<br />';
				}
				echo $permInfo;
				?>
			</div>
		</div>
		<br /><hr />
		<div class="row">
			<h3><?php echo Yii::t('admin', 'Settings'); ?></h3>
			<?php
			Yii::app()->clientScript->registerScript('empty-contact-showhide', "
				(function($) {
					$(\"[name='Admin[emailDropbox_createContact]']\").change(function() {
						if($(this).is(':checked'))
							$('#empty-contact').fadeIn(300);
						else
							$('#empty-contact').fadeOut(300);
					});
				})(jQuery);");
			$form = $this->beginWidget('CActiveForm', array(
				'id' => 'settings-form',
				'enableAjaxValidation' => false,
					));
			$htmlOptions = array('style'=>'display:inline-block;margin-right: 10px;');
			?>
			<?php
			echo $form->labelEx($model, 'emailDropbox_alias',$htmlOptions);
			echo CHtml::tag('span', array('class' => 'x2-hint','style'=>$htmlOptions['style'], 'title' => Yii::t('admin', 'The address to use as the sender when sending error notification emails. By default, if left blank, the email dropbox will use the first addresses in the {tohf} or {cchf} field that contains {dbat}.', array('{tohf}' => 'To:', '{cchf}' => 'CC:', '{dbat}' => '"dropbox@"'))), '[?]',$htmlOptions);
			echo $form->textField($model, 'emailDropbox_alias',$htmlOptions);

			?>
			<br />
			<?php
			echo $form->labelEx($model, 'emailDropbox_createContact',$htmlOptions);
			echo CHtml::tag('span', array('class' => 'x2-hint','style'=>$htmlOptions['style'], 'title' => Yii::t('admin', 'If disabled, the email dropbox will ignore any emails that are to or from addresses not matching any contacts in X2CRM. If enabled, new contacts will be created automatically using name info contained in the email.')), '[?]',$htmlOptions);
			echo $form->checkBox($model, 'emailDropbox_createContact',$htmlOptions);
			?>
			<br />
			<div style="margin-left:20px;<?php echo (bool) $model->emailDropbox_createContact ? '' : 'display:none'; ?>" id="empty-contact">
				<?php
				echo $form->labelEx($model, 'emailDropbox_emptyContact',$htmlOptions);
				echo CHtml::tag('span', array('class' => 'x2-hint','style'=>$htmlOptions['style'], 'title' => Yii::t('admin', "If enabled, the email dropbox will create a new contact record associated with a new unique email address even if the first and last name cannot be found in the email. If disabled, it ignores all email that does not contain contacts' first and last names. This setting has no effect if {ccfe} is disabled.", array('{ccfe}' => '"' . Yii::t('admin', 'Create contacts from emails') . '"'))), '[?]',$htmlOptions);
				echo $form->checkBox($model, 'emailDropbox_emptyContact',$htmlOptions);
				?>
			</div>
			<?php
			echo $form->labelEx($model, 'emailDropbox_zapLineBreaks',$htmlOptions);
			echo CHtml::tag('span', array('class' => 'x2-hint','style'=>$htmlOptions['style'], 'title' => Yii::t('admin', 'If enabled, the mail parser will (when extracting the body of an email) attempt to clear the text of artificial line breaks induced by RFC email format specifications (which limit lines to 78 characters). If disabled, the email parser will not do this.')), '[?]',$htmlOptions);
			echo $form->checkBox($model, 'emailDropbox_zapLineBreaks',$htmlOptions);
			?>
			<br />
			<?php
			echo $form->labelEx($model, 'emailDropbox_logging',$htmlOptions);
			echo CHtml::tag('span', array('class' => 'x2-hint','style'=>$htmlOptions['style'], 'title' => Yii::t('admin', 'If enabled, the email dropbox will record email capture events in a log file in protected/runtime. This option is useful for troubleshooting but will take up some extra disk space on a system that captures a high volume of emails.')), '[?]',$htmlOptions);
			echo $form->checkBox($model, 'emailDropbox_logging',$htmlOptions);
			?>
			<br /><hr />
			<?php
			echo CHtml::submitButton(Yii::t('app', 'Save'), array('class' => 'x2-button btn btn-mini  btn-yellow', 'id' => 'save-button')) . "\n";
			$this->endWidget();
			?>
		</div>
	</div>
</div>